package com.burger.counter.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.burger.counter.Constants.SQLConstants;
import com.burger.counter.beans.Cheese;
import com.burger.counter.util.DBConnection;

      public class CheeseDao {
	    public Cheese[] getCheeseWithoutPriceDetails(int regionId){
	        Connection con=null;
	        ResultSet rs=null;
	        java.sql.PreparedStatement prest =null;
	        Cheese CheeseList[]=null;
	        int size;
	        try{
	            con=DBConnection.getConnection();
	            System.out.println("Creating DB Connection");
	            String prepStatement=SQLConstants.cheeseListWithoutPrice;
	            prest = con.prepareStatement(prepStatement);
	            prest.setInt(1,regionId);
	            
	            rs = prest.executeQuery();
	            if (rs != null) {
	                rs.last();
	                size = rs.getRow();
	                CheeseList = new Cheese[size];
	                int currRow = 0;
	                rs.beforeFirst();

	                while (rs.next()) {
	                    Cheese p = new Cheese();
	                    p.setCheeseId(rs.getInt(1));
	                    p.setCheeseName(rs.getString(2));
	                    CheeseList[currRow] = p;
	                    currRow++;
	                }
	            }        

	        }
	        catch(Exception e){
	            
	        }
	        
	        return CheeseList;
	    }

	    
	    public Cheese[] getCheeseWithPriceDetails(int regionId){
	        Connection con=null;
	        ResultSet rs=null;
	        java.sql.PreparedStatement prest =null;
	        Cheese CheeseList[]=null;
	        int size;
	        try{
	            con=DBConnection.getConnection();
	            System.out.println("Creating DB Connection");
	            String prepStatement=SQLConstants.cheeseListWithPrice;
	            prest = con.prepareStatement(prepStatement);
	            prest.setInt(1,regionId);
	            
	            rs = prest.executeQuery();
	            if (rs != null) {
	                rs.last();
	                size = rs.getRow();
	                CheeseList = new Cheese[size];
	                int currRow = 0;
	                rs.beforeFirst();

	                while (rs.next()) {
	                    Cheese p = new Cheese();
	                    p.setCheeseId(rs.getInt(1));
	                    p.setCheeseName(rs.getString(2));
	                    p.setCheeseExtraPrice(rs.getDouble(3));
	                    CheeseList[currRow] = p;
	                    currRow++;
	                }
	            }        

	        }
	        catch(Exception e){
	            
	        }
	        
	        return CheeseList;
	    }
		public void setCheeseCount(int regionId,String cheeseName){
			Connection con=null;
			int rs;
			PreparedStatement prest =null;
			try{
				con=DBConnection.getConnection();
				System.out.println("Creating DB Connection");
				String prepStatement=SQLConstants.updateCheeseCount;
				prest = con.prepareStatement(prepStatement);
				prest.setInt(1, regionId);
				prest.setString(2, cheeseName);
				
				rs = prest.executeUpdate();
				if (rs == 1) {
					System.out.println("Cheese Count has been updated");
				}					

			}
			catch(Exception e){
				
			}		
		}
	}
	


	